package com.kun.valiant.assignment.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.valiant.assignment.domain.Assignment;
import com.kun.valiant.assignment.mapper.AssignmentMapper;
import com.kun.valiant.assignment.service.IAssignmentService;
import com.kun.valiant.common.constant.ValiantConstants;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 分配记录表 服务实现类
 * </p>
 *
 * @author fangkun
 * @since 2023-06-08
 */
@Service
public class AssignmentServiceImpl extends ServiceImpl<AssignmentMapper, Assignment> implements IAssignmentService {

    @Override
    @Transactional
    public AjaxResult assignClue(Assignment assignment) {
        //1.将一个线索的分配记录的latest设为false
        LambdaUpdateWrapper<Assignment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Assignment::getLatest, false).eq(Assignment::getAssignId,assignment.getAssignId());
        update(updateWrapper);
        //2.分配线索
//        assignment.setType(ValiantConstants.CLUE_TYPE);
        assignment.setCreateBy(SecurityUtils.getUsername());
        assignment.setCreateTime(LocalDateTime.now());
        assignment.setLatest(true);
        save(assignment);
        return AjaxResult.success("线索分配成功");
    }
}
